package org.erepublik.company.controllers;

import java.util.ArrayList;
import java.util.Arrays;

import org.erepublik.company.CountryData;
import org.erepublik.company.EAlliance;
import org.erepublik.company.ECountry;
import org.erepublik.company.EWorld;
import org.erepublik.company.Location;
import org.erepublik.company.comparators.CountryDataComparatorByName;
import org.erepublik.company.product.Food;
import org.erepublik.company.searchCriteria.CountrySearchCriteria;
import org.erepublik.company.searchCriteria.EAllianceSearchCriteria;
import org.erepublik.company.service.JSONCountryDataProvider;
import org.erepublik.company.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/countryData.html")
public class CountryDataController extends AbstractController {
    
    @Autowired // autumatycznie wstrzykiwany obiekt
    private JSONCountryDataProvider dataService;
    
    @RequestMapping(method = RequestMethod.GET)
    public String showForm(ModelMap model) {
        fillModelMap(model);
        CountryData[] countryData = dataService.getAllCountryData();
        Arrays.sort(countryData, new CountryDataComparatorByName());
        model.addAttribute("countryData", countryData);
        model.addAttribute("updateTime", dataService.getMinutesSinceUpdate());
        model.addAttribute("countryDataSearchCriteria", new CountrySearchCriteria());
        return "countryData";
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String showList(ModelMap model, @ModelAttribute("countryDataSearchCriteria") CountrySearchCriteria criteria) {
        fillModelMap(model);
        CountryData[] countryData = dataService.getAllCountryData();
        countryData = filterAlliances(criteria.getAlliance(), countryData);
        Arrays.sort(countryData, criteria.getSortCriteria().getComparator() ) ;
        
        model.addAttribute("countryData", countryData);
        model.addAttribute("updateTime", dataService.getMinutesSinceUpdate());
        model.addAttribute("countryDataSearchCriteria", criteria);
        return "countryData";
    }

    private CountryData[] filterAlliances(EAllianceSearchCriteria alliance, CountryData[] products) {

        if (alliance == EAllianceSearchCriteria.ALL)
            return products;
        
        ArrayList<CountryData> list = new ArrayList<CountryData>();
        for (CountryData food : products) {
            
            if (food.getLocation()==EWorld.GLOBAL)
                continue;
            EAlliance all = getAlliance(food.getLocation());
            
            if (food.getLocation() instanceof ECountry) {
                if (  Utils.contains( all, alliance.getAlliances() ) )
                list.add(food);
                continue;
            }
            
         
        }
        return list.toArray(new CountryData[0]);
    }

    private EAlliance getAlliance(Location location) {
        if (location instanceof EAlliance)
            return (EAlliance)location;
        
        return ((ECountry)location).getAlliance();
    }
    
  
}
